import waves from "@/directive/waves";
import Pagination from "@/components/Pagination";
import { alertWithCallback, alertMessage, isValidateNull } from "@/utils/utils";
import { mapGetters } from 'vuex';
import { getDataList, deleteSelectedData, save } from "./dictionary-management.service";

export default {
  name: 'dictionary-management',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      recordNum: 20, // 分页组件默认的页数, 分页里的select默认10, 20, 50
      totalNum: 0, // 数据总数
      pageNum: 1, // 页数
      queryString: "", // 搜索字段
      dataList: [], // 用户列表
      loading: true, // table loading
      detailDialogVisible: false,
      selectedDataDetail: {}, // 选中用户详情
      dialogTitle: '', // 新建用户、编辑用户
      editDialogVisible: false,
      editRules: {
        tableName: [{ required: true, trigger: "change", validator: isValidateNull }],
        itemValue: [{ required: true, trigger: "change", validator: isValidateNull }],
        itemName: [{ required: true, trigger: "change", validator: isValidateNull }]
      }, // 表单规则
      editForm: {}, // 表单对象
      checkedRows: [] // table checkbox选中数据
    }
  },
  computed: {
    ...mapGetters([
      'codesCheck'
    ])
  },
  created() {
    this.getDataList();
  },
  methods: {
    // 获取列表
    getDataList() {
      this.loading = false;
      getDataList(this.queryString, this.pageNum, this.recordNum).then(res => {
        this.totalNum = res.dataCount;
        this.dataList = res.data;
        this.loading = false;
      });
    },

    // 根据关键字进行搜索
    search() {
      this.pageNum = 1;
      this.getDataList();
    },

    // 重置搜索
    reset() {
      this.pageNum = 1;
      this.queryString = '';
      this.getDataList();
    },

    // 删除单条选中数据
    deleteSelectedData() {
      if (this.checkedRows.length == 0) {
        alertMessage('请选择记录', 'warning');
      } else {
        alertWithCallback('此操作将永久删除该数据, 是否继续?', '提示').then(() => {
          const ids = [];
          this.checkedRows.forEach(item => {
            ids.push(item.objectId)
          });
          deleteSelectedData(ids).then((res) => {
            alertMessage(res.message);
            this.reset();
          }).catch(() => {}); // 关闭confirm框的回调，不处理的话会报一个js error
        }).catch(() => {}); // 关闭confirm框的回调，不处理的话会报一个js error
      }
    },

    // 打开新建、编辑窗体
    openEditDetailDialog(type, selectedData) {
      if (type === 'create') {
        this.dialogTitle = '新建字典';
        // 重置必填项
        setTimeout(() => {
          this.$refs.dataForm.resetFields();
          this.editForm = {};
          this.imageUrl = '';
          this.fileList = [];
        }, 100);
      } else {
        this.dialogTitle = '编辑字典';
        this.$nextTick(() => {
          this.editForm = Object.assign({}, selectedData);
          this.$refs.dataForm.resetFields();
        })
      }
      this.editDialogVisible = true;
    },

    // 创建或者编辑用户信息
    save() {
      // 表单验证
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          save(this.editForm).then((res) => {
            this.editDialogVisible = false;
            this.reset();
            alertMessage(res.message);
          }).catch(() => {}); // 关闭confirm框的回调，不处理的话会报一个js error
        }
      });
    },

    // 处理checkbox选中行数据
    handleSelectionChange(val) {
      this.checkedRows = val;
    }
  }
}
